package org.dingfugui.triple.protocol.mssage;

import org.dingfugui.triple.protocol.Translator;
import org.dingfugui.triple.rpc.RequestMessage;
import org.dingfugui.triple.rpc.ResponseMessage;

/**
 * 默认的消息处理器，直接用序列化的对象作为报文体
 * 请求报文格式
 * 报文头(4):报文长度(3)+类型(1)
 * 请求报文体(n):
 * 序列化的对象
 * 响应报文体(n):
 * result(n)
 * Created by fugui.ding on 2015/6/8.
 */
public abstract class AbstractDefaultTranslator implements Translator {


    /**
     * 获取请求报文体
     *
     * @param request
     * @return
     */
    abstract byte[] buildMessageBody(RequestMessage request);

    /**
     * 获取响应报文体
     *
     * @param response
     * @return
     */
    abstract byte[] buildMessageBody(ResponseMessage response);

    /**
     * 根据报文体获得请求参数
     *
     * @param body
     * @return
     */
    abstract RequestMessage buildRequest(byte[] body);

    /**
     * 根据报文体获得响应参数
     *
     * @param body
     * @return
     */
    abstract ResponseMessage buildResponse(byte[] body);


    @Override
    public int getMessageHeadSize() {
        return 4;
    }

    @Override
    public byte[] encodeRequest(RequestMessage request) {
        byte[] body = buildMessageBody(request);

        byte[] msg = new byte[body.length + getMessageHeadSize()];
        msg[0] = (byte) (msg.length & 0xFF);
        msg[1] = (byte) (msg.length >>> 8);
        msg[2] = (byte) (msg.length >>> 16);

        System.arraycopy(body, 0, msg, 4, body.length);
        return msg;
    }

    @Override
    public byte[] encodeResponse(ResponseMessage response) {
        byte[] body = buildMessageBody(response);

        byte[] msg = new byte[body.length + getMessageHeadSize()];
        msg[0] = (byte) (msg.length & 0xF);
        msg[1] = (byte) (msg.length >>> 8);
        msg[2] = (byte) (msg.length >>> 16);

        System.arraycopy(body, 0, msg, 4, body.length);
        return msg;
    }

    @Override
    public ResponseMessage decodeResponse(byte[] data) {
        int headSize = getMessageHeadSize();
        byte[] msgArray = new byte[data.length - headSize];//去掉报文头
        System.arraycopy(data, headSize, msgArray, 0, data.length - headSize);
        ResponseMessage message = buildResponse(msgArray);

        return message;
    }

    @Override
    public RequestMessage decodeRequest(byte[] data) {
        int headSize = getMessageHeadSize();
        byte[] msgArray = new byte[data.length - headSize];//去掉报文头
        System.arraycopy(data, headSize, msgArray, 0, data.length - headSize);
        RequestMessage message = buildRequest(msgArray);
        return message;
    }

     /*@Override
    public byte[] handle(byte[] data) {
        System.out.println("处理报文:" + data.length);

        int headSize = getMessageHeadSize();

        byte[] msgArray = new byte[data.length - headSize];//去掉报文头

        System.arraycopy(data, headSize, msgArray, 0, data.length - headSize);

        RequestMessage message = decodeRequest(msgArray);

        Event event = new Event(message.getService(), message.getMethod(), message.getTypes(), message.getParams());
        event = executeHandler.handle(event);

        Object result = event.getResult();

        ResponseMessage responseMessage = new ResponseMessage();
        responseMessage.setResult(result);

        byte[] body = encodeResponse(responseMessage);

        byte[] resultArray = new byte[headSize + body.length];

        resultArray[0] = (byte) (body.length & 0xF);
        resultArray[1] = (byte) (body.length >>> 8);
        resultArray[2] = (byte) (body.length >>> 16);

        System.arraycopy(body, 0, resultArray, 4, body.length);

        return resultArray;
    }*/

}
